package karma.main;

import net.minecraft.src.*;

public class TileEntityEnchanter extends TileEntity implements IInventory
{
    private ItemStack EnchanterItemStacks[];
    public int EnchanterBurnTime;
    public int currentItemBurnTime;
    public int EnchanterCookTime;

    public TileEntityEnchanter()
    {
        EnchanterItemStacks = new ItemStack[3];
        EnchanterBurnTime = 0;
        currentItemBurnTime = 0;
        EnchanterCookTime = 0;
    }

    /**
     * Returns the number of slots in the inventory.
     */
    public int getSizeInventory()
    {
        return EnchanterItemStacks.length;
    }

    /**
     * Returns the stack in slot i
     */
    public ItemStack getStackInSlot(int i)
    {
        return EnchanterItemStacks[i];
    }

    /**
     * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
     * new stack.
     */
    public ItemStack decrStackSize(int i, int j)
    {
        if (EnchanterItemStacks[i] != null)
        {
            if (EnchanterItemStacks[i].stackSize <= j)
            {
                ItemStack itemstack = EnchanterItemStacks[i];
                EnchanterItemStacks[i] = null;
                return itemstack;
            }

            ItemStack itemstack1 = EnchanterItemStacks[i].splitStack(j);

            if (EnchanterItemStacks[i].stackSize == 0)
            {
                EnchanterItemStacks[i] = null;
            }

            return itemstack1;
        }
        else
        {
            return null;
        }
    }

    /**
     * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
     * like when you close a workbench GUI.
     */
    public ItemStack getStackInSlotOnClosing(int i)
    {
        if (EnchanterItemStacks[i] != null)
        {
            ItemStack itemstack = EnchanterItemStacks[i];
            EnchanterItemStacks[i] = null;
            return itemstack;
        }
        else
        {
            return null;
        }
    }

    /**
     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
     */
    public void setInventorySlotContents(int i, ItemStack itemstack)
    {
        EnchanterItemStacks[i] = itemstack;

        if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
        {
            itemstack.stackSize = getInventoryStackLimit();
        }
    }

    /**
     * Returns the name of the inventory.
     */
    public String getInvName()
    {
        return "container.Enchanter";
    }

    /**
     * Reads a tile entity from NBT.
     */
    public void readFromNBT(NBTTagCompound nbttagcompound)
    {
        super.readFromNBT(nbttagcompound);
        NBTTagList nbttaglist = nbttagcompound.getTagList("Items");
        EnchanterItemStacks = new ItemStack[getSizeInventory()];

        for (int i = 0; i < nbttaglist.tagCount(); i++)
        {
            NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(i);
            byte byte0 = nbttagcompound1.getByte("Slot");

            if (byte0 >= 0 && byte0 < EnchanterItemStacks.length)
            {
                EnchanterItemStacks[byte0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
            }
        }

        EnchanterBurnTime = nbttagcompound.getShort("BurnTime");
        EnchanterCookTime = nbttagcompound.getShort("CookTime");
        currentItemBurnTime = getItemBurnTime(EnchanterItemStacks[1]);
    }

    /**
     * Writes a tile entity to NBT.
     */
    public void writeToNBT(NBTTagCompound nbttagcompound)
    {
        super.writeToNBT(nbttagcompound);
        nbttagcompound.setShort("BurnTime", (short)EnchanterBurnTime);
        nbttagcompound.setShort("CookTime", (short)EnchanterCookTime);
        NBTTagList nbttaglist = new NBTTagList();

        for (int i = 0; i < EnchanterItemStacks.length; i++)
        {
            if (EnchanterItemStacks[i] != null)
            {
                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
                nbttagcompound1.setByte("Slot", (byte)i);
                EnchanterItemStacks[i].writeToNBT(nbttagcompound1);
                nbttaglist.appendTag(nbttagcompound1);
            }
        }

        nbttagcompound.setTag("Items", nbttaglist);
    }

    /**
     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
     * this more of a set than a get?*
     */
    public int getInventoryStackLimit()
    {
        return 64;
    }

    public int getCookProgressScaled(int i)
    {
        return (EnchanterCookTime * i) / 100;
    }

    public int getEnchantTimeRemainingScaled(int i)
    {
        if (currentItemBurnTime == 0)
        {
            currentItemBurnTime = 100;
        }

        return (EnchanterBurnTime * i) / currentItemBurnTime;
    }

    public boolean isEnchanting()
    {
        return EnchanterBurnTime > 0;
    }

    /**
     * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count
     * ticks and creates a new spawn inside its implementation.
     */
    public void updateEntity()
    {
        boolean flag = EnchanterBurnTime > 0;
        boolean flag1 = false;

        if (EnchanterBurnTime > 0)
        {
            EnchanterBurnTime--;
        }

        if (!worldObj.isRemote)
        {
            if (EnchanterBurnTime == 0 && canSmelt())
            {
                currentItemBurnTime = EnchanterBurnTime = getItemBurnTime(EnchanterItemStacks[1]);

                if (EnchanterBurnTime > 0)
                {
                    flag1 = true;

                    if (EnchanterItemStacks[1] != null)
                    {
                        if (EnchanterItemStacks[1].getItem().func_46056_k())
                        {
                            EnchanterItemStacks[1] = new ItemStack(EnchanterItemStacks[1].getItem().setFull3D());
                        }
                        else
                        {
                            EnchanterItemStacks[1].stackSize--;
                        }

                        if (EnchanterItemStacks[1].stackSize == 0)
                        {
                            EnchanterItemStacks[1] = null;
                        }
                    }
                }
            }

            if (isEnchanting() && canSmelt())
            {
                EnchanterCookTime++;

                if (EnchanterCookTime == 100)
                {
                    EnchanterCookTime = 0;
                    smeltItem();
                    flag1 = true;
                }
            }
            else
            {
                EnchanterCookTime = 0;
            }

            if (flag != (EnchanterBurnTime > 0))
            {
                flag1 = true;
                BlockEnchanter.updateEnchanterBlockState(EnchanterBurnTime > 0, worldObj, xCoord, yCoord, zCoord);
            }
        }

        if (flag1)
        {
            onInventoryChanged();
        }
    }

    private boolean canSmelt()
    {
        if (EnchanterItemStacks[0] == null)
        {
            return false;
        }

        ItemStack itemstack = EnchanterRecipes.enchanting().getEnchantingResult(EnchanterItemStacks[0].getItem().shiftedIndex);

        if (itemstack == null)
        {
            return false;
        }

        if (EnchanterItemStacks[2] == null)
        {
            return true;
        }

        if (!EnchanterItemStacks[2].isItemEqual(itemstack))
        {
            return false;
        }

        if (EnchanterItemStacks[2].stackSize < getInventoryStackLimit() && EnchanterItemStacks[2].stackSize < EnchanterItemStacks[2].getMaxStackSize())
        {
            return true;
        }
        else
        {
            return EnchanterItemStacks[2].stackSize < itemstack.getMaxStackSize();
        }
    }

    public void smeltItem()
    {
        if (!canSmelt())
        {
            return;
        }

        ItemStack itemstack = EnchanterRecipes.enchanting().getEnchantingResult(EnchanterItemStacks[0].getItem().shiftedIndex);

        if (EnchanterItemStacks[2] == null)
        {
            EnchanterItemStacks[2] = itemstack.copy();
        }
        else if (EnchanterItemStacks[2].itemID == itemstack.itemID)
        {
            EnchanterItemStacks[2].stackSize += itemstack.stackSize;
        }

        if (EnchanterItemStacks[0].getItem().func_46056_k())
        {
            EnchanterItemStacks[0] = new ItemStack(EnchanterItemStacks[0].getItem().setFull3D());
        }
        else
        {
            EnchanterItemStacks[0].stackSize--;
        }

        if (EnchanterItemStacks[0].stackSize <= 0)
        {
            EnchanterItemStacks[0] = null;
        }
    }

    private int getItemBurnTime(ItemStack itemstack)
    {
        if (itemstack == null)
        {
            return 0;
        }

        int i = itemstack.getItem().shiftedIndex;

        if (i == mod_KarmaCraft.MagicPowder.shiftedIndex)
        {
            return 100;
        }
        else
        {
            return ModLoader.addAllFuel(itemstack.itemID, itemstack.getItemDamage());
        }
    }

    /**
     * Do not make give this method the name canInteractWith because it clashes with Container
     */
    public boolean isUseableByPlayer(EntityPlayer entityplayer)
    {
        if (worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) != this)
        {
            return false;
        }
        else
        {
            return entityplayer.getDistanceSq((double)xCoord + 0.5D, (double)yCoord + 0.5D, (double)zCoord + 0.5D) <= 64D;
        }
    }

    public void openChest()
    {
    }

    public void closeChest()
    {
    }
}
